import React, { useMemo, useState } from "react";
import EditPage, { createFields } from "../../Component/ResourceList/Edit";
import { ActivityPerpose } from "./Data";
import { SelectExpertFormItem } from "../Activity/SourceData";
import { isArray } from "lodash";
const ApplyEdit = () => {
    const [ defaultRegion ] = useState(() => {
        const userData = JSON.parse(sessionStorage.getItem("AdminData") || "{}");
        return [userData.province, userData.city, userData.area];
    });
    const fields = useMemo(() =>createFields([{
            label: "地区",
            name: "region",
            type: "Region",
            required: true,
            props: {
                defaultValue: defaultRegion,
                disabled: true
            }
        },
        {
            label: "单位名称",
            name: "hostOrganization",
            type: "Input",
            required: true,
            rules: [{
                "type": "string",
                "max": 200,
                "min": 1,
                "message": "请输入5-200个字符"
            }]
        },{
            label: "活动目的",
            name: "activityPurpose",
            type: "CheckGroup",
            required: true,
            props: {
                options: ActivityPerpose
            }
        },{
            label: "规模",
            name: "userNums",
            type: "Input",
            required: true,
            props: {
                type: "number"
            }
        }, {
            label: "需求内容",
            name: "requirement",
            type: "TextArea",
            required: true
        }, {
            label: "服务时段",
            name: "activityPeriod",
            type: "Input",
            required: true,
        }, {
            label: "活动地点",
            name: "address",
            type: "Input",
            required: true,
        }, {
            label: "活动联系人",
            name: "contactor",
            type: "Input",
            required: true,
        }, {
            label: "联系电话",
            name: "mobile",
            type: "Input",
            required: true
        }, {
            label: "专家",
            name: "expert",
            type: "Custom",
            Component: SelectExpertFormItem
        }
    ]),[defaultRegion]);
    return (
        <EditPage
            submitEndPoint="activity.applySubmit"
            fields={fields}
            initValues={{
                region: defaultRegion
            }}
            buildResponse={(response: any) => {
                return {
                    ...response,
                    region: response.region ? (typeof response.region === "string" ? JSON.parse(response.region) : response.region) : [],
                    activityPurpose: response.activityPurpose ? JSON.parse(response.activityPurpose) : [],
                    expert: response.resources || []
                }
            }}
            buildRequest={(formData: any) => {
                const expertList = isArray(formData.expert) ? formData.expert?.map((v: any) => v.id) : [];
                return {
                    ...formData,
                    status: 0,
                    region: JSON.stringify(formData.region),
                    activityPurpose: JSON.stringify(formData.activityPurpose),
                    province: formData.region[0],
                    city: formData.region[1],
                    area: formData.region[2],
                    expert: JSON.stringify(expertList)
                };
            }}
        />
    )
};

export default ApplyEdit;
